/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.OrderParameterCarrier;
import com.minierp.bean.QueryParameterCarrier;
import com.minierp.bean.QueryResult;
import com.minierp.bean.UserBean;
import com.minierp.controller.DbBackupThread;
import com.minierp.dao.DbBackupDao;
import com.minierp.dao.EnterpriseInfoDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.QueryDao;
import com.minierp.err.PermissionDennyException;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.PageHelper;
import com.minierp.model.DbBackup;
import com.minierp.model.EnterpriseInfo;
import java.io.File;
import java.util.HashMap;
import java.util.Vector;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class DbBackupMACtr extends MultiActionController {
	private QueryDao queryDao;
	private DbBackupDao dbBackupDao;
	private EnterpriseInfoDao enterpriseInfoDao;
	private IdGenerator idGenerator;
	private String listView;
	private String formView;

	public ModelAndView handleList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (!EncCommonHelper.checkPrivilege(request, "DB_BACK")) {
			throw new PermissionDennyException("Permission Denny");
		} else {
			ModelAndView mv = new ModelAndView(this.listView);
			String objName = "com.minierp.model.DbBackup";
			String objShortName = "b";
			HashMap shortNameMap = new HashMap();
			String[] retObjs = new String[]{objShortName};
			Vector qpVe = new Vector();
			shortNameMap.put(objName, objShortName);
			QueryParameterCarrier[] qParas = new QueryParameterCarrier[qpVe
					.size()];
			OrderParameterCarrier p1 = new OrderParameterCarrier();
			p1.objectName = objName;
			p1.propertyName = "id";
			p1.orderTarget = "desc";
			OrderParameterCarrier[] oParas = new OrderParameterCarrier[]{p1};
			int currentPage = EncCommonHelper.getCurrentPageIndex(request);
			QueryResult qr = this.queryDao.doQuery(currentPage,
					EncCommonHelper.getPageSize(request), qParas, oParas,
					(String[]) null, shortNameMap, retObjs, (String[]) null);
			mv.addObject("qr", qr);
			mv.addObject("ent", this.enterpriseInfoDao.get());
			return mv;
		}
	}

	public ModelAndView handleDelete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String returnUrl = request.getParameter("from_url");
		ModelAndView mv = new ModelAndView("redirect:" + returnUrl);
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		if (ub.getUser().getUserType() != 1) {
			throw new Exception("Permission deny!");
		} else {
			String idStr = request.getParameter("id_str");
			String[] idArr = idStr.split(";");

			for (int i = 0; i < idArr.length; ++i) {
				long id = Long.parseLong(idArr[i]);
				DbBackup b = (DbBackup) this.dbBackupDao
						.get(DbBackup.class, id);
				if (b != null) {
					String fileName = b.getFileName();
					int index = fileName.lastIndexOf(File.separator);
					String folder = fileName.substring(0, index);
					Runtime r = Runtime.getRuntime();
					Process p = null;
					String command = "cmd /C rd " + folder + " /s/q";

					try {
						p = r.exec(command);
						p.waitFor();
					} finally {
						if (p != null && p.getInputStream() != null) {
							p.getInputStream().close();
							p.getOutputStream().close();
							p.getErrorStream().close();
							p.destroy();
							p = null;
						}

					}
				}

				this.dbBackupDao.delete(id);
			}

			EncCommonHelper.setAlertMessage(request, "删除成功");
			return mv;
		}
	}

	public ModelAndView handleNew(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.formView);
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		if (!EncCommonHelper.checkPrivilege(request, "DB_BACK")) {
			throw new Exception("Permission deny!");
		} else {
			EnterpriseInfo ent = EncCommonHelper.getEnterpriseInfo(request);
			String[] fileNameArr = PageHelper.getDbBackupFileName(
					this.getServletContext(), ent);
			DbBackup b = new DbBackup();
			String stafferName = "";
			long stafferId = 0L;
			if (ub.getStaffer() != null) {
				stafferName = ub.getStaffer().getStafferName();
				stafferId = ub.getStaffer().getId();
			}

			b.setId(this.idGenerator.getId(DbBackup.class.getName()));
			b.setCreaterId(stafferId);
			b.setCreaterName(stafferName);
			String copyName = "";
			if (fileNameArr[1] != null && !fileNameArr[1].equals("")) {
				copyName = fileNameArr[0];
				b.setFileName(fileNameArr[1]);
			} else {
				b.setFileName(fileNameArr[0]);
			}

			this.dbBackupDao.save(b);
			String mysqlPath = PageHelper.getMysqlBinPath(this
					.getServletContext());
			String dbPort = PageHelper.getDbPort(this.getServletContext());
			DbBackupThread t = new DbBackupThread(b.getId(), this.dbBackupDao,
					mysqlPath, dbPort, copyName);
			t.start();
			mv.addObject("id", String.valueOf(b.getId()));
			mv.addObject("showMsg", request.getParameter("show_msg"));
			return mv;
		}
	}

	public ModelAndView handleCheck(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		DbBackup b = (DbBackup) this.dbBackupDao.get(DbBackup.class,
				Long.parseLong(id));
		String completeFlag = "N";
		String size = "0";
		if (b != null) {
			completeFlag = b.getCompleteFlag();
			File out = new File(b.getFileName());
			size = String.valueOf(out.length());
		}

		response.setContentType("text/plain");
		response.setCharacterEncoding("utf-8");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Expires", "0");
		ServletOutputStream out1 = response.getOutputStream();
		out1.write((completeFlag + ";" + size).getBytes());
		out1.flush();
		out1.close();
		return null;
	}

	public DbBackupDao getDbBackupDao() {
		return this.dbBackupDao;
	}

	public void setDbBackupDao(DbBackupDao dbBackupDao) {
		this.dbBackupDao = dbBackupDao;
	}

	public String getFormView() {
		return this.formView;
	}

	public void setFormView(String formView) {
		this.formView = formView;
	}

	public String getListView() {
		return this.listView;
	}

	public void setListView(String listView) {
		this.listView = listView;
	}

	public QueryDao getQueryDao() {
		return this.queryDao;
	}

	public void setQueryDao(QueryDao queryDao) {
		this.queryDao = queryDao;
	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public EnterpriseInfoDao getEnterpriseInfoDao() {
		return this.enterpriseInfoDao;
	}

	public void setEnterpriseInfoDao(EnterpriseInfoDao enterpriseInfoDao) {
		this.enterpriseInfoDao = enterpriseInfoDao;
	}
}